﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Keepo.Helpers;
using System.Web.Routing;

namespace Keepo.Helpers
{
    //this FilterAttribute checks if the current user has the specified permission
    public class HasPermissionAttribute : ActionFilterAttribute
    {
        public int[] UserPermissionIds { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            if (!UserPermissionIds.Any(p => UserHelper.HasPermission(p)))
            {
                filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
                {
                    controller = "Home",
                    action = "NoPermission"
                }));
            }
        }
    }
}